package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.behavior.dtos.LikeBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;


    /**
     * 点赞与取消点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeBehavior(LikeBehaviorDto dto) {

        if (dto == null || (dto.getOperation() == null || dto.getOperation() < 0 || dto.getOperation() > 1) || (dto.getType() == null || dto.getType() < 0 || dto.getType() > 2)) {
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数异常");
        }

        ApUser apUser = ApThreadLocalUtil.get();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String key = "LIEK_BEHAVIOR_"+dto.getArticleId();

        if(dto.getOperation()==0){      //点赞操作

            if(cacheService.hGet(key,apUser.getId()+"")!=null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"您已经点赞了");
            }

            cacheService.hPut(key,apUser.getId()+"", JSON.toJSONString(dto));


            //将点赞行为发送到kafka后面要作实时流处理
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setArticleId(dto.getArticleId());
            updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            updateArticleMess.setAdd(1);

            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(updateArticleMess));


        }else{          //取消点赞
            cacheService.hDelete(key,apUser.getId()+"");

            //将点赞行为发送到kafka后面要作实时流处理
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setArticleId(dto.getArticleId());
            updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            updateArticleMess.setAdd(-1);

            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(updateArticleMess));
        }



        return ResponseResult.okResult(null);
    }


    /**
     * 记录阅读行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        ApUser apUser = ApThreadLocalUtil.get();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        String key="READ_BEHAVIOR_"+dto.getArticleId();
        //如果首页阅读则将信息记录到redis中
        String json = (String)cacheService.hGet(key, apUser.getId() + "");
        if(StringUtils.isBlank(json)){
            cacheService.hPut(key,apUser.getId()+"",JSON.toJSONString(dto));
        }else{
            //否则将阅读的次数加1
            ReadBehaviorDto readBehaviorDto = JSON.parseObject(json, ReadBehaviorDto.class);
            readBehaviorDto.setCount(readBehaviorDto.getCount()+1);
            cacheService.hPut(key,apUser.getId()+"",JSON.toJSONString(readBehaviorDto));
        }


        //将点赞行为发送到kafka后面要作实时流处理
        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setArticleId(dto.getArticleId());
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        updateArticleMess.setAdd(1);

        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(updateArticleMess));



        return ResponseResult.okResult(null);
    }


    /**
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        ApUser apUser = ApThreadLocalUtil.get();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        String key = "COLLECTION_BEHAVIOR_"+dto.getEntryId();
        if(dto.getOperation()==0){
            if(cacheService.hGet(key,apUser.getId()+"")!=null){
                ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已收藏");
            }

            cacheService.hPut(key,apUser.getId()+"",JSON.toJSONString(dto));


            //将点赞行为发送到kafka后面要作实时流处理
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setArticleId(dto.getEntryId());
            updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
            updateArticleMess.setAdd(1);

            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(updateArticleMess));


        }else{
            cacheService.hDelete(key,apUser.getId()+"");

            //将点赞行为发送到kafka后面要作实时流处理
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setArticleId(dto.getEntryId());
            updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
            updateArticleMess.setAdd(-1);

            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(updateArticleMess));
        }

        return ResponseResult.okResult(null);
    }
}
